package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.entity.yifei.Coptd;
import com.example.entity.yifei.Copth;
import com.example.mapper.CoptdMapper;
import com.example.mapper.CopthMapper;
import com.example.service.CopthService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiangPandeng
 * @since 2024-04-17
 */
@Service
public class CopthServiceImpl extends ServiceImpl<CopthMapper, Copth> implements CopthService {

    @Autowired
    private CopthMapper copthMapper;

    @Autowired
    private CoptdMapper coptdMapper;

    /**
     * 0 未出  1 销货中 2 已销完
     * @param TC001
     * @param TC002
     * @return
     */
    @Override
    public Integer getCoptcStatus(String TC001, String TC002) {
        Integer count = coptdMapper.selectCount(new LambdaQueryWrapper<Coptd>()
                .eq(Coptd::getTd001, TC001).eq(Coptd::getTd002, TC002));
        if(count == 0) return 0;
        List<Copth> copths = copthMapper.checkedList(TC001, TC002);
        Set<String> set = new HashSet<>();
        for(Copth copth : copths) {
            set.add(new String(copth.getTH014() + "&" +
                    copth.getTH015() + "&" + copth.getTH016()));
        }
        if(set.size() == 0) return 0;
        if(count == set.size()) return 2;
        return 1;
    }
}
